<template>
  <div class="view-wrapper">
    <div class="head">
      <el-form :model="queryParams" ref="queryForm" :inline="true">
        <el-form-item label="" prop="keyword">
          <el-input v-model="queryParams.keyword" placeholder="客户名称/商家名称/林客ID" clearable style="width: 260px;" />
        </el-form-item>
       
        <el-form-item label="客户渠道：" prop="clientSource">
          <el-select v-model="queryParams.clientSource" placeholder="请选择" style="width: 240px;">
            <el-option v-for="item in sourceList" :key="item.id" :label="item.value" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="客户负责人：" prop="responsiblePersonIdList">
          <el-select v-model="queryParams.responsiblePersonIdList" multiple collapse-tags placeholder="请选择" style="width: 240px;">
            <el-option v-for="item in responsiblePersonList" :key="item.id" :label="item.value" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="查询时间：">
          <el-date-picker clearable v-model="date" type="daterange" format="yyyy-MM-dd" value-format="yyyy-MM-dd"
            range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" @change="changeTime" placeholder="请选择">
          </el-date-picker>
        </el-form-item> -->
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="handleQuery">查询</el-button>
          <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="listBox">
      <div class="listBox_head" style="margin: 0 0 20px">
        <div>
          <el-date-picker
            v-model="date"
            type="monthrange"
            format="yyyy-MM" 
            value-format="yyyy-MM"
            range-separator="至"
            start-placeholder="开始月份"
            end-placeholder="结束月份"
            @change="changeTime"> 
          </el-date-picker>
        </div>
        <div>
          
          <el-button type="primary" @click="writeOffUpLoad">数据导入</el-button>
          <export-excel-button title="月度销售汇总" action="/client/month/sales/summary/list/export" :type="'2'"
          :params="queryParams" />
        </div>
      </div>
      <el-table v-loading="loading" ref="multipleTable" :data="tabList" stripe border show-summary :summary-method="getSummaries" tooltip-effect="dark" style="width: 100%">
        <el-table-column label="序号" align="center" type="index" width="64px" />
        <el-table-column label="客户名称" align="center" prop="clientName"></el-table-column>
        <el-table-column label="商家名称" align="center" prop="dyLkMerchantName"></el-table-column>
        <el-table-column label="商家ID" align="center" prop="dyLkMerchantId"></el-table-column>
        <el-table-column label="客户负责人" align="center" prop="responsiblePerson"></el-table-column>
        <el-table-column label="渠道名称/其他备注" align="center" prop="sourceRemark"></el-table-column>
        <el-table-column label="客户级别" align="center" prop="clientLevel"></el-table-column>
        <el-table-column label="支付GMV" align="center" prop="payGmv"></el-table-column>
        <el-table-column label="核销GMV" align="center" prop="verificationGmv"></el-table-column>
        <el-table-column label="退款GMV" align="center" prop="refundGmv"></el-table-column>
        <el-table-column label="支付券数" align="center" prop="payCoupon"></el-table-column>
        <el-table-column label="核销券数" align="center" prop="verificationCoupon"></el-table-column>
        <el-table-column label="退款券数" align="center" prop="refundCoupon"></el-table-column>
        <el-table-column label="总预估佣金" align="center" prop="totalCommission"></el-table-column>
        <el-table-column label="服务商预估佣金" align="center" prop="serviceCommission"></el-table-column>
        <el-table-column label="操作" align="center" fixed="right" style="width: 240px;">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="lookDetail(scope.row)"  v-if="scope.row.clientId != '-'">客户详情</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
        @pagination="getList"></pagination>
    </div>

    <!-- 直播结果弹窗 -->
    <SalesDialog v-if="dialogVisible" :id="dialogId" :pathType="pathType" :visible.sync="dialogVisible"
      @success="_handleSuccess">
    </SalesDialog>
  </div>
</template>

<script>
import {
  getList,
} from "@/api/customer/moonSale";
import {
  responsibleSelect,
} from "@/api/customer/customerFile";
import SalesDialog from "./components/salesDialog";
import ExportExcelButton from "@/components/ExportExcelButton/index";
export default {
  name: "",
  components: {
    SalesDialog,
    ExportExcelButton
  },
  data() {
    return {
      dialogId: '',
      dialogVisible: false,
      watchVisible: false,
      // 遮罩层
      loading: false,
      // 导出遮罩层
      // 总条数
      total: 0,
      //表格数据
      tabList: [],
      pathType: 'add',
      date: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        keyword: '',//查询关键字（客户名称/商家名称/林客id）
        clientSource: '',//客户来源 1 自开发 2 渠道商 3 其他 bee_client_source
        responsiblePersonIdList: [],//客户负责人
        beginMonth: '',//开始月份
        endMonth: '',//结束月份
      },
      shopType: "",
      responsiblePersonList: [],
      sourceList: [
        { id: '', value: '全部' },
      ],
    };
  },
  computed: {

  },
  created() {
    this.getDict()
    this.getresponsiblePersonList()
    this.getList();
  },
  methods: {
   
     //获取状态字典
    getDict() {
      var dictType = "bee_client_source";
      this.$getSelectList(dictType)
        .then((res) => {
    
          this.sourceList = this.sourceList.concat(res.result[0].dictList) 
        
        })
        .finally(() => {
          this.loading = false;
        });
        console.log('this.industryList',this.industryList)
    },

    /** 查询列表 */
    getList() {
      
      this.loading = true;
      var data = this.queryParams
      // let data = JSON.parse(JSON.stringify(this.queryParams));  //深拷贝
      // data.personIds = data.personIds.toString()
      getList(data).then(response => {
        this.tabList = response.result.records;
        if(this.tabList.length > 0){
          this.date = []
          this.date.push(this.tabList[0].beginMonth)
          this.date.push(this.tabList[0].endMonth)
          console.log(this.date)
        }
        this.tabList = this.tabList.map(item => {
          Object.keys(item).forEach(key => {
            if (item[key] === '') {
              item[key] = '-'
            }
          })
          return item
        })
        this.total = response.result.total;
        this.loading = false;
      });
    },
    //合计
    getSummaries(param){
        const { columns, data } = param;
        const sums = [];
        columns.forEach((column, index) => {
          if (index === 0) {
            sums[index] = '合计';
            return;
          }
          else if(index===1||index===2||index===3||index===4){
            sums[index] = '-';
            return;
          }
          const values = data.map(item => Number(item[column.property]));
          if (!values.every(value => isNaN(value))) {
            sums[index] = values.reduce((prev, curr) => {
              const value = Number(curr);
              if (!isNaN(value)) {
                return Math.round((Number(prev) + Number(curr)) * 100) / 100;
              } else {
                return prev;
              }
            }, 0);
          } else {
            sums[index] = '-';
          }
        });
        return sums;
    },
    //客户负责人下拉
    getresponsiblePersonList() {
      responsibleSelect().then((res) => {
        this.responsiblePersonList = res.result
      })
        .finally(() => {
          this.loading = false;
        });
    },

    //处理时间
    changeTime(e) {
      console.log('e',e)
      this.queryParams.beginMonth = e[0]
      this.queryParams.endMonth = e[1]
      this.getList()
    },
    // 表单重置
    reset() {
      this.date = []
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        beginMonth: '',
        endMonth: '',
        keyword: '',
        clientSource: '',
        responsiblePersonIdList: [],
      };
      this.resetForm("queryParams");
      this.handleQuery()
    },
    salesUpLoad() {
      this.dialogVisible = true;
      this.pathType = 'sales'
    },
    writeOffUpLoad() {
      this.dialogVisible = true;
      this.pathType = 'write'
    },
    //客户详情
    lookDetail(row) {
      this.$router.push({
          path: "/customerFileDetail",
          query: {
            id: row.clientId,
            clientName: row.clientName,
            pathType: 'index'
          },
        })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      // this.resetForm("queryForm");
      this.reset();
    },
    _handleSuccess(e) {
      //成功回调
      this.resetQuery()
    },
  }
};

</script>
<style scoped>
.head {
  box-shadow: 0px 4px 12px 0px rgb(1 41 99 / 6%);
  background-color: #fff;
  border-radius: 8px;
  padding: 20px 20px 0;
}

.listBox {
  box-shadow: 0px 4px 12px 0px rgb(1 41 99 / 6%);
  background-color: #fff;
  border-radius: 8px;
  padding: 20px 20px;
  margin-top: 10px;
}

.listBox_head {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.img_box {
  width: 100px;
  height: 80px;
  border-radius: 4px;
}

.contentTitle {
  width: 100%;
  display: flex;
}

.ellipsis {
  display: -webkit-box;
  text-overflow: ellipsis;
  overflow: hidden;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

.el-popover {
  height: auto;
  color: #fff;
  background: rgba(0, 0, 0, 0.8);
}
</style>
